import 'package:flutter/material.dart';
import 'package:osc_flutter/components/TextAvatarAnimation.dart';

class TextAvatarContainer extends StatefulWidget {
  TextAvatarContainer(this.backgroundWidth, this.text, this.icon, this.color,
      this.endEdgeInsets,this.onTap);

  final double backgroundWidth;
  final String text;
  final Icon icon;
  final Color color;
  final EdgeInsets endEdgeInsets;
  final VoidCallback onTap;

  TextAvatarContainerState state;

  @override
  TextAvatarContainerState createState() {
    state = new TextAvatarContainerState();
    return state;
  }

  TextAvatarContainerState getState() {
    return state;
  }
}

class TextAvatarContainerState extends State<TextAvatarContainer>
    with TickerProviderStateMixin {
  AnimationController _controller;
  bool _opened = false;

  switchAnimation() {
    _playAnimation();
  }

  @override
  void initState() {
    super.initState();

    _controller = new AnimationController(
        duration: const Duration(milliseconds: 300), vsync: this);
    switchAnimation();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  void _playAnimation() {
    if (!_opened) {
      _controller.forward();
    } else {
      _controller.reverse();
    }
    _opened = !_opened;
  }

/*  Future<Null> _playAnimation() async {
    if (!_opened) {
      try {
        await _controller.forward().orCancel;
      } on TickerCanceled {}
    } else {
      try {
        await _controller.reverse().orCancel;
      } on TickerCanceled {}
    }

    _opened = !_opened;
  }*/

  @override
  Widget build(BuildContext context) {
    TextAvatarAnimation avatarAnimation = new TextAvatarAnimation(
      controller: _controller.view,
      backgroundWidth: this.widget.backgroundWidth,
      text: this.widget.text,
      icon: this.widget.icon,
      color: this.widget.color,
      endEdgeInsets: this.widget.endEdgeInsets,
    );
    return new GestureDetector(
      child: avatarAnimation,
      onTap: this.widget.onTap,
    );
  }
}
